'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 竞技场战报
 * @author agl
 */

var _ = require('lodash');
var moment = require('moment');
var async = require('async');
var modelUtil = require('../util/modelUtil');
var Code = require('../consts/code.js');
var mongoConnector = require('../libs/mongodb/mongoConnector');
/* jshint unused:false */
var arenaBattleRecordSchema = require('../schemas/arenaBattleRecordSchema.js');
var ArenaBattleRecord = mongoConnector().model('ArenaBattleRecordSchema');

/**
 * 根据角色id获取竞技场数据
 */
module.exports.findByRoleId = function (role_id, cb) {
    ArenaBattleRecord.find({
        $or: [{
            atk_role_id: role_id
        }, {
            def_role_id: role_id
        }]
    }).sort({
        create_tm: -1
    }).exec(cb);
};

/**
 * 创建战报
 */
module.exports.createBattle = function (role_id, rival_id, atk_info, def_info, cb) {
    ArenaBattleRecord.create({
        atk_role_id: role_id,
        def_role_id: rival_id,
        atk: atk_info,
        def: def_info
    }, cb);
};

/**
 * 查询战报
 */
module.exports.findById = function (battle_id, cb) {
    ArenaBattleRecord.findOne({
        battle_id: battle_id
    }).exec(cb);
};

/**
 * 设置战斗结果
 */
module.exports.setBattleResult = function (battle_id, battle_result, rank_changed, cb) {
    ArenaBattleRecord.update({
        battle_id: battle_id
    }, {
        battle_end: 1,
        battle_result: battle_result,
        rank_changed: rank_changed
    }).exec(cb);
};
